/*
题目链接:https://leetcode.cn/problems/push-dominoes/solutions/?envType=daily-question&envId=2025-05-02
*/

//题解代码:
class Solution {
public:
    #define INF 1e9
    string pushDominoes(string dominoes) {
        int n = dominoes.size();
        vector<int> left(n,-INF),right(n,INF);
        for(int i=1;i<n;++i){
            if(dominoes[i-1] == 'R') left[i] = i-1;
            else if(dominoes[i-1] == 'L') continue;
            else left[i] = left[i-1];
        }
        for(int i=n-2;i>=0;--i){
            if(dominoes[i+1] == 'L') right[i] = i+1;
            else if(dominoes[i+1] == 'R') continue;
            else right[i] = right[i+1];
        }
        string ans;
        for(int i=0;i<n;++i){
            if(dominoes[i] != '.') ans += dominoes[i];
            else{
                if(left[i]!= -INF && right[i]!=INF){
                    int llen = i - left[i];
                    int rlen = right[i] - i;
                    if(llen == rlen) ans += '.';
                    else ans += (llen>rlen ? 'L' : 'R');
                }else if(left[i] != -INF){
                    ans += 'R';
                }else if(right[i] != INF){
                    ans += 'L';
                }else{
                    ans += '.';
                }
            }
        }
        return ans;
    }
};
